package com.lanou.renrenxin.singleton;

import android.database.sqlite.SQLiteDatabase;

import com.android.volley.RequestQueue;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.ImageRequest;
import com.android.volley.toolbox.NetworkImageView;
import com.android.volley.toolbox.Volley;
import com.lanou.renrenxin.R;
import com.lanou.renrenxin.base.BaseApplication;
import com.lanou.renrenxin.dao.ChatList;
import com.lanou.renrenxin.dao.ChatListDao;
import com.lanou.renrenxin.dao.DaoMaster;
import com.lanou.renrenxin.dao.DaoSession;
import com.lanou.renrenxin.tools.RenrenCache;

import java.util.ArrayList;
import java.util.List;

import de.greenrobot.dao.query.QueryBuilder;

/**
 * Created by zouhui on 16/3/4.
 */
public class RenrenxinSingleton {
    private DaoMaster daoMaster;
    private SQLiteDatabase sqLiteDatabase;
    private DaoSession daoSession;
    private RenrenCache renrenCache;
    private ImageLoader imageLoader;
    private RequestQueue requestQueue;

    private static class SingleHolder {
        private static RenrenxinSingleton instence = new RenrenxinSingleton();
    }

    public static RenrenxinSingleton getInstence() {
        return SingleHolder.instence;
    }

    public RenrenxinSingleton() {
        daoMaster = getDaoMaster();
        sqLiteDatabase = getSqLiteDatabase();
        daoSession = getDaoSession();
        renrenCache = getRenrenCache();
        imageLoader = getImageLoader();
        requestQueue = getRequestQueue();
    }

    public ImageLoader getImageLoader() {
        if (null == imageLoader) {
            return imageLoader = new ImageLoader(getRequestQueue(),getRenrenCache());
        }
        return imageLoader;
    }

    public RenrenCache getRenrenCache() {
        if (null == renrenCache) {
            renrenCache = new RenrenCache();
        }
        return renrenCache;
    }

    public RequestQueue getRequestQueue() {
        if (null == requestQueue) {
            return requestQueue = Volley.newRequestQueue(BaseApplication.getContext());
        }
        return requestQueue;
    }

    public DaoMaster getDaoMaster() {
        if (null == daoMaster) {
            return daoMaster = new DaoMaster(getSqLiteDatabase());
        }
        return daoMaster;
    }

    public DaoSession getDaoSession() {
        if (null == daoSession) {
            return daoSession = getDaoMaster().newSession();
        }
        return daoSession;
    }

    public SQLiteDatabase getSqLiteDatabase() {
        if (null == sqLiteDatabase) {
            DaoMaster.OpenHelper helper = new DaoMaster.DevOpenHelper(BaseApplication.getContext(), "Renrenxin.db", null);
            sqLiteDatabase = helper.getWritableDatabase();
            return sqLiteDatabase;
        }
        return sqLiteDatabase;
    }

    public List<ChatList> queryChatList(String mUsername) {
        List<ChatList> chatLists = new ArrayList<>();
        QueryBuilder builder = getInstence().getDaoSession().getChatListDao().queryBuilder();
        builder.where(ChatListDao.Properties.MUsername.eq(mUsername));
        chatLists = builder.list();
        return chatLists;
    }

    public void saveChatList(String mUsername, String username, String body, String time, int type, int status, String msgType) {
        getInstence().getDaoSession().getChatListDao().insertOrReplace(new ChatList(mUsername, username, body, type, time, status, msgType));
    }

    public void addImageRequest(ImageRequest imageRequest){
        getInstence().getRequestQueue().add(imageRequest);
    }
    public void loadNetWorkImage(NetworkImageView networkImageView,String url){
        networkImageView.setErrorImageResId(R.mipmap.erro_img);
        networkImageView.setDefaultImageResId(R.mipmap.defult_img);
        networkImageView.setImageUrl(url,getImageLoader());
    }


}
